package com.cloud.lan.integration.submail;

import com.alibaba.fastjson.JSON;
import com.cloud.lan.integration.submail.common.CommonService;
import com.cloud.lan.integration.submail.mms.entity.MmsContentElement;
import com.cloud.lan.integration.submail.mms.entity.MmsSendReq;
import com.cloud.lan.integration.submail.mms.entity.MmsSendRes;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateContent;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateHandleRes;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateModel;
import com.cloud.lan.integration.submail.mms.entity.MmsTemplateQueryRes;
import com.cloud.lan.integration.submail.mms.service.MmsSendService;
import com.cloud.lan.integration.submail.mms.service.MmsTemplateService;
import com.cloud.lan.integration.submail.mms.service.impl.MmsSendServiceImpl;
import com.cloud.lan.integration.submail.mms.service.impl.MmsTemplateServiceImpl;
import com.cloud.lan.integration.submail.sms.entity.SmsSendModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouquan on 2022/4/2.
 */
public class MmsTestDemo {

    public static void main(String[] args)throws Exception{
        //彩信模板测试
        testMmsTemplate();

        //彩信发送测试
        //testMmsSend("V6xzj3");
    }


    private static void testMmsSend(String templateId){
        //校验模板
        MmsTemplateService mmsTemplateService = new MmsTemplateServiceImpl();
        MmsTemplateModel model = new MmsTemplateModel();
        model.setTemplateId(templateId);
        MmsTemplateQueryRes queryRes = mmsTemplateService.query(model);
        System.out.println("查询" +JSON.toJSONString(queryRes));
        MmsTemplateModel templateModel = queryRes.getTemplate();
        Assert.isTrue(StringUtils.equals(templateId,templateModel.getTemplateId())
                && StringUtils.equals("2",templateModel.getTemplateStatus()),"模板无效");

        //发送
        MmsSendService sendService = new MmsSendServiceImpl();
        //模板方式批量发送短信
        MmsSendReq sendReq = new MmsSendReq();
        sendReq.setTemplateId(templateId);

        List<SmsSendModel> modelList = new ArrayList<>();
        //用户1
        SmsSendModel ssm1 = new SmsSendModel();
        ssm1.setTo("13761231450");
        Map<String,String> vars1 = new HashMap<>();
        vars1.put("lanDays","3");
        vars1.put("lanNum","30");
        ssm1.setVars(vars1);
        modelList.add(ssm1);
        //用户2
        SmsSendModel ssm2 = new SmsSendModel();
        ssm2.setTo("13681937143");
        Map<String,String> vars2 = new HashMap<>();
        vars2.put("lanDays","4");
        vars2.put("lanNum","40");
        ssm2.setVars(vars2);
        modelList.add(ssm2);
        //用户3
        SmsSendModel ssm3 = new SmsSendModel();
        ssm3.setTo("13671617811");
        Map<String,String> vars3 = new HashMap<>();
        vars3.put("lanDays","5");
        vars3.put("lanNum","50");
        ssm3.setVars(vars3);
        modelList.add(ssm3);
        //用户4
        SmsSendModel ssm4 = new SmsSendModel();
        ssm4.setTo("13813948485");
        Map<String,String> vars4 = new HashMap<>();
        vars4.put("lanDays","6");
        vars4.put("lanNum","60");
        ssm4.setVars(vars4);
        modelList.add(ssm4);

        sendReq.setModelList(modelList);
        List<MmsSendRes> resList = sendService.multixsend(sendReq);
        System.out.println("模板方式批量发送彩信:"+JSON.toJSONString(resList));
    }

    private static void testMmsTemplate()throws Exception{
        CommonService commonService = new CommonService();
        MmsTemplateService mmsTemplateService = new MmsTemplateServiceImpl();
        MmsTemplateModel model = new MmsTemplateModel();
        model.setMmsTitle("兰嘉丝汀小程序");
        model.setMmsSubject("兰嘉丝汀小程序");
        model.setMmsSignature("【兰嘉丝汀】");
        List<MmsTemplateContent> contentList = new ArrayList<>();
        MmsTemplateContent content1 = new MmsTemplateContent();
        content1.setText("提醒：有最新款式的护肤产品将在@var(lanDays)天后上架，总计数量@var(lanNum)件，先到先得请及时登录小程序购物");
        MmsContentElement element1 = new MmsContentElement();
        element1.setType("image/jpg");
        element1.setData(commonService.encodeBase64File("/Users/zhouquan/Downloads/spu_new.jpg"));
        content1.setImage(element1);
        contentList.add(content1);
        model.setContentList(contentList);

        //创建
        MmsTemplateHandleRes handleRes = mmsTemplateService.create(model);
        System.out.println("创建" +JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"创建失败");
        String templateId =  String.copyValueOf(handleRes.getTemplateId().toCharArray());
        Assert.isTrue(StringUtils.isNotBlank(templateId),"创建失败");

        //查询
        MmsTemplateQueryRes queryRes = mmsTemplateService.query(model);
        System.out.println("创建查询" +JSON.toJSONString(queryRes));
        long nums = queryRes.getTemplates().stream().filter(tmp->StringUtils.equals(templateId,tmp.getTemplateId()) && StringUtils.equals(model.getMmsSignature(),tmp.getMmsSignature())).count();
        Assert.isTrue(nums>0,"创建查询失败");

        //更新
        model.setTemplateId(templateId);
        model.setMmsTitle("ZQ联调更新");
        model.setMmsSubject("ZQ联调更新");
        model.setMmsSignature("【ZQ联调更新】");
        handleRes = mmsTemplateService.update(model);
        System.out.println("更新" +JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"更新失败");

        //查询
        queryRes = mmsTemplateService.query(model);
        System.out.println("更新查询" +JSON.toJSONString(queryRes));
        MmsTemplateModel templateModel = queryRes.getTemplate();
        Assert.isTrue(StringUtils.equals(templateId,templateModel.getTemplateId()) && StringUtils.equals(model.getMmsSignature(),templateModel.getMmsSignature()),"更新查询失败");

        //删除
        handleRes = mmsTemplateService.del(model);
        System.out.println("删除" +JSON.toJSONString(handleRes));
        Assert.isTrue(StringUtils.equals("success",handleRes.getStatus()),"删除失败");

        //查询
        model.setTemplateId(null);
        queryRes = mmsTemplateService.query(model);
        System.out.println("删除查询" +JSON.toJSONString(queryRes));
        nums = queryRes.getTemplates().stream().filter(tmp->StringUtils.equals(templateId,tmp.getTemplateId())).count();
        Assert.isTrue(nums==0,"删除查询失败");
    }
}
